package com.example.my_flea_street.controller;

import com.example.my_flea_street.bean.*;
import com.example.my_flea_street.service.SearchService;
import com.example.my_flea_street.util.ResponseHelper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping(value = "/search")
public class SearchController {
    private final SearchService searchService;

    @Autowired
    public SearchController(SearchService searchService) {
        this.searchService = searchService;
    }

    @RequestMapping(value = "/getHotKeys", method = {RequestMethod.POST, RequestMethod.GET})
    public ResponseEntity<ResponseModel> Login() {
        return ResponseHelper.createResponse(0,"成功",searchService.getHotKeys());
    }

    @RequestMapping(value = "/searchInfo", method = {RequestMethod.POST, RequestMethod.GET})
    public ResponseEntity<ResponseModel> searchInfo(@RequestBody Map<String, String> json ) {
        String key = json.get("key");
        int pageNum = Integer.parseInt(json.get("page_no"));
        int pageSize = Integer.parseInt(json.get("page_size"));
        PageHelper.startPage(pageNum,pageSize);
        PageInfo<Info> pageInfo = new PageInfo<>(searchService.searchInfo(key));
        InfoPage page = new InfoPage();
        page.setTotal((int)pageInfo.getTotal());
        page.setPages(pageInfo.getPages());
        page.setPageNum(pageInfo.getPageNum());
        page.setData(pageInfo.getList());
        return ResponseHelper.createResponse(0, "成功",page);
    }
    @RequestMapping(value = "/searchUser", method = {RequestMethod.POST, RequestMethod.GET})
    public ResponseEntity<ResponseModel> searchUser(@RequestBody Map<String, String> json ) {
        String key = json.get("key");
        int pageNum = Integer.parseInt(json.get("page_no"));
        int pageSize = Integer.parseInt(json.get("page_size"));
        PageHelper.startPage(pageNum,pageSize);
        PageInfo<SearchUser> pageInfo = new PageInfo<>(searchService.searchUser(key));
        SearchUserPage page = new SearchUserPage();
        page.setTotal((int)pageInfo.getTotal());
        page.setPages(pageInfo.getPages());
        page.setPageNum(pageInfo.getPageNum());
        page.setData(pageInfo.getList());
        return ResponseHelper.createResponse(0, "成功",page);
    }
}
